package it.tref.eclipse.wicket.plugin.editors;

import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditorActionContributor;
import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.ide.IDEActionFactory;
import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;


/**
 * Manages the installation/deinstallation of global actions for multi-page editors.
 * Responsible for the redirection of global actions to the active editor.
 * Multi-page contributor replaces the contributors for the individual editors in the multi-page editor.
 * 
 * The actions has been reoved and replaced with plugin extensions to allow key bindings
 */
@SuppressWarnings("restriction")
public class WicketFunEditorContributor extends MultiPageEditorActionBarContributor
{
	private CompilationUnitEditorActionContributor contributor;
	
	@Override
	public void init(IActionBars bars)
	{
		contributor = new CompilationUnitEditorActionContributor();
		contributor.init(bars, getPage());
		
		super.init(bars);
	}
	
	/**
	 * Creates a multi-page contributor.
	 */
	public WicketFunEditorContributor() {
		super();
	}
	
	@Override
	public void setActivePage(IEditorPart part)
	{
		contributor.setActiveEditor(part);
		getActionBars().updateActionBars();
		
		IActionBars actionBars = getActionBars();
		if (actionBars != null) {

			ITextEditor editor = (part instanceof ITextEditor) ? (ITextEditor) part : null;
			
			actionBars.setGlobalActionHandler(
				ActionFactory.DELETE.getId(),
				getAction(editor, ITextEditorActionConstants.DELETE));
			actionBars.setGlobalActionHandler(
				ActionFactory.UNDO.getId(),
				getAction(editor, ITextEditorActionConstants.UNDO));
			actionBars.setGlobalActionHandler(
				ActionFactory.REDO.getId(),
				getAction(editor, ITextEditorActionConstants.REDO));
			actionBars.setGlobalActionHandler(
				ActionFactory.CUT.getId(),
				getAction(editor, ITextEditorActionConstants.CUT));
			actionBars.setGlobalActionHandler(
				ActionFactory.COPY.getId(),
				getAction(editor, ITextEditorActionConstants.COPY));
			actionBars.setGlobalActionHandler(
				ActionFactory.PASTE.getId(),
				getAction(editor, ITextEditorActionConstants.PASTE));
			actionBars.setGlobalActionHandler(
				ActionFactory.SELECT_ALL.getId(),
				getAction(editor, ITextEditorActionConstants.SELECT_ALL));
			actionBars.setGlobalActionHandler(
				ActionFactory.FIND.getId(),
				getAction(editor, ITextEditorActionConstants.FIND));
			actionBars.setGlobalActionHandler(
				IDEActionFactory.BOOKMARK.getId(),
				getAction(editor, IDEActionFactory.BOOKMARK.getId()));
			actionBars.setGlobalActionHandler(
					"RulerDoubleClick",
					getAction(editor, ITextEditorActionConstants.RULER_DOUBLE_CLICK));
			
			actionBars.updateActionBars();
		}
	}
	
	private IAction getAction(ITextEditor editor, String action) {
		return editor.getAction(action);
	}
}